#!/bin/bash
set -e
if [ "x${1}" = "xsubbuild" ]; then
	#如果是上层调用该脚本
	if [ $# -eq 1 ]; then
		#功能码999表示查看帮助
		export fun_code=999
	elif [ $# -eq 2 ]; then
		#获取功能码
		export fun_code=$2
	else
    	echo -e "\033[47;41m子模块参数错误!\033[0m"
    	echo -e "\033[47;41m子模块参数参考: $0 subbuild 调用码 功能码\033[0m"
		exit 1
	fi
    echo -e "\033[30;43m继承环境变量!\033[0m"
else
    echo -e "\033[30;43m配置环境变量!\033[0m"
	#>>-----------------------------------------------------------------------------------
	#切换目录
	cd ../
	#-------------------------------------------------------------------------------------

	#>>-----------------------------------------------------------------------------------
	#顶层项目目录
	export PROJECT_TOPDIR=$PWD
	#顶层当前配置文件
	export CURCFG_FILE=${PROJECT_TOPDIR}/.curconfig.sh
	#-------------------------------------------------------------------------------------

	#>>env--------------------------------------------------------------------------------
	#设置颜色函数
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.000.colorsetup.sh > /dev/null 
	#导入公共变量
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.001.envsetup_common.sh > /dev/null 
	#导入平台变量
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.002.envsetup_platform.sh > /dev/null 
	#-------------------------------------------------------------------------------------

	#检查当前机型配置
	if [ ! -f ${CURCFG_FILE} ]; then
		serro "检查当前配置文件... [失败]"
		exit 1
	fi
	source ${CURCFG_FILE} > /dev/null 

	#>>-----------------------------------------------------------------------------------
	#切回目录
	cd -> /dev/null
	#-------------------------------------------------------------------------------------

	if [ $# -eq 1 ]; then
		export fun_code=$1
	else
		export fun_code=999
	fi
fi
echo -e "\033[47;42m当前功能码: ${fun_code}\033[0m"

#-----------------------------------------------------------------------
export MODPKG_ROOT=${MGRKIT_TOPDIR}/pkg
export MODSRC_ROOT=${MGRKIT_TOPDIR}/src

export MODNAME=bcsysmgr
export MODPKG_NAME=${MODNAME}.tar.gz 
export MODSRC_NAME=${MODNAME}

export MODPKG_PATH=${MODPKG_ROOT}/${MODPKG_NAME}
export MODSRC_PATH=${MODSRC_ROOT}/${MODSRC_NAME}
export MODOUT_PATH=${MGRKIT_TOPDIR}/out/z.output.${MODNAME}

export MODREF_PATH=${RCSKIT_TOPDIR}/src/rc.local

export MODCPY1_ROOT=${PROJECT_TOPDIR}/z.src.rtfs/usr/local/sbin
export MODCPY2_ROOT=${PROJECT_TOPDIR}/z.out.rtfs/usr/local/sbin
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
#导入公共函数
source 000.import.commonfunc.sh > /dev/null
#-----------------------------------------------------------------------
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
function build_module()
{
	if [ ! -d ${MODOUT_PATH} ]; then
		mkdir -p ${MODOUT_PATH}
	fi

	(cd ${MODSRC_PATH} && make )
    [ $? -ne 0 ] && serro "编译开始: [失败]" && exit 1
	sdone "编译模块: [完成]"

	sinfo "安装模块: [开始]"
	(cd ${MODSRC_PATH} && make install)
    [ $? -ne 0 ] && serro "安装模块: [失败]" && exit 1
	sdone "安装模块: [完成]"
}
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
function clean_module()
{
	sinfo "清除模块: [开始]"
	if [ -f ${MODSRC_PATH}/Makefile ]; then
		(cd ${MODSRC_PATH} && make distclean)
		[ $? -ne 0 ] && serro "清除模块: [失败]"	&& exit 1
	fi
	sdone "清除模块: [完成]"
}
#-----------------------------------------------------------------------

function showusage()
{
	echo "使用说明:"
	echo "$0 [功能码]"
	echo "$0  1       ${MODNAME}源码编译"
	echo "$0  2       ${MODNAME}清除编译"
	echo "$0  3       ${MODNAME}源码打包"
	echo "$0  4       ${MODNAME}安装到文件系统"
	echo "$0  999     查看本帮助"
}
#-----------------------------------------------------------------------
function mainproc() 
{
	if [ "x$fun_code" = "x1" ]; then
		check_env
		check_source
		
		if [ ! -d ${MODREF_PATH} ]; then
			perro "查找rc.local源码 ... [失败]"
			return 1
		fi

		ckcfile=${MODREF_PATH}/ual_types.h
		if [ ! -f ${ckcfile} ]; then
			perro "查找文件: ${ckcfile} [失败]"
			return 1
		fi

		ckcfile=${MODREF_PATH}/psn_conf.h
		if [ ! -f ${ckcfile} ]; then
			perro "查找文件: ${ckcfile} [失败]"
			return 1
		fi

		ckcfile=${MODREF_PATH}/device_i2ceeprom.h
		if [ ! -f ${ckcfile} ]; then
			perro "查找文件: ${ckcfile} [失败]"
			return 1
		fi

		export PKG_CONFIG_PATH=${MODOUT_PATH}/lib/pkgconfig
		export LDFLAGS="-L${MODOUT_PATH}/lib"
		export CFLAGS="-I${MODSRC_PATH} -I${MODOUT_PATH}/include -I${MODREF_PATH} -static"
		clean_module
		build_module
	elif [ "x$fun_code" = "x2" ]; then
		check_env
		clean_module
	elif [ "x$fun_code" = "x3" ]; then
		pack_module
	elif [ "x$fun_code" = "x4" ]; then
		if [ ! -d ${MODCPY1_ROOT} ]; then
			mkdir -p ${MODCPY1_ROOT}
		fi
		if [ ! -d ${MODCPY2_ROOT} ]; then
			mkdir -p ${MODCPY2_ROOT}
		fi
		swarn "cp -rf \t${MODOUT_PATH}/${MODNAME} \n\t${MODCPY1_ROOT}/${MODNAME}"
		cp -rf ${MODOUT_PATH}/${MODNAME} ${MODCPY1_ROOT}/${MODNAME}

		sdone "cp -rf \t${MODOUT_PATH}/${MODNAME} \n\t${MODCPY2_ROOT}/${MODNAME}"
		cp -rf ${MODOUT_PATH}/${MODNAME} ${MODCPY2_ROOT}/${MODNAME}
	else
		showusage $@
	fi
}

mainproc $@
